<?php
/**
 * Hello Model for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
 * @license    GNU/GPL
 */
 
// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.model' );
 
/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class NvModelHotel extends JModel
{
    /**
    * Gets the greeting
    * @return string The greeting to be displayed to the user
    */	
	var $_data;
	
    function getDatas()
    {	   
	// Lets load the data if it doesn't already exist
	    if (empty( $this->_data ))
	    {
	        $query = $this->_buildQuery();
			$tangkap = $this -> _getword();
	        $this->_data = $this->_getList( $query );
				
				
	// ------------------------------- melakukan pengambilan nilai id propinsi dan id kota -------------------------------------
					for ($i=0, $n=count( $this->_data ); $i < $n; $i++)
				    {
						$row =& $this->_data[$i];
				        $id_kota = $row->kota_id;
				        $id_propinsi = $row->propinsi_id;
				    }

        // mencari nama kota dari objek pariwisata
        $query = "select nama_kota from #__kota where kota_id ='$id_kota' and propinsi_id = '$id_propinsi'";
        $this->_data = $this->_getList($query);

                    for ($i=0, $n=count( $this->_data ); $i < $n; $i++)
				    {
						$row =& $this->_data[$i];
				        $nama_kota = $row->nama_kota;
				    }

    //------------------------- melakukan pencarian dalam tabel kota jika pencarian dalam tabel nusaview tidak membawa hasil -----------------------
        if (!$nama_kota && $tangkap ){

        $nama_kota = $this->_getword();

        }



	// --------------------------------- Mengambil url rss dari tabel hotel ----------------------------------------------------				
					$query = "select url_rss from #__hotel where kota like '%".$nama_kota."%'";
					$this->_data = $this->_getList( $query );
	    
		}
	// ------------  akhir dari if empty -------


        // -------------------------------------- menangkap url rss dari database dan diletakkan pada variabel ---------------------------------------
            for ($i=0, $n=count( $this->_data ); $i < $n; $i++)
                {
                    $row =& $this->_data[$i];
                    $url_rss = $row->url_rss;
                }

        // -------------------------------------- mengambil rss -----------------------------------------------------------------------------

        $options = array('rssUrl' => $url_rss);
        $rssDoc =& JFactory::getXMLparser('RSS', $options);
	
	    return $rssDoc;
		
    } // end of function getDatas
	
	function _buildQuery()
	{		
		$tangkap = $this->_getword();
		
		// --------------------- melakukan pencarian text yang diinputkan di dalam tabel nusaview ------------------------------
	    $query = "select kota_id,propinsi_id from #__nusaview where name like '%".$tangkap."%'";
	    return $query;
	}
	
	function _getword(){
	
		//----------------------------  menangkap text yang diinputkan oleh user -------------------------------------------------
		$tangkap = $_POST[field_cari];
		
		return $tangkap;
	}

    function getKota(){

		 $db =& JFactory::getDBO();
		//prepare the query
		 $query = "SELECT id,kota from #__hotel";

		 $db->setQuery($query);
		 $rows = $db->loadObjectList();


		//data array for storing tourism object
		$data = array();
		$index = 0;

		foreach ($rows as $row){

			$data[$index++] = $row;
		}

		//mengembalikan data
		return $data;

	}

    function getHotel($namaKota){
	

	// --------------------------------- Mengambil url rss dari tabel hotel ----------------------------------------------------
        $query = "select url_rss from #__hotel where kota like '%".$namaKota."%'";
        $this->_data = $this->_getList( $query );


    // -------------------------------------- menangkap url rss dari database dan diletakkan pada variabel ---------------------------------------
        for ($i=0, $n=count( $this->_data ); $i < $n; $i++)
            {
                $row =& $this->_data[$i];
                $url_rss = $row->url_rss;
            }

        // -------------------------------------- mengambil rss -----------------------------------------------------------------------------

        $options = array('rssUrl' => $url_rss);
        $rssDoc =& JFactory::getXMLparser('RSS', $options);

	    return $rssDoc;
    }
	
	
}
